Completed
Push — master ( 2c40ae...326927 )
by Fike
38s
created

Factory.spec.js ➔ describe(ꞌIntegrationꞌ)   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 103

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 103
rs 8.2857

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/* eslint-env mocha */
2
3
var Sinon = require('sinon')
4
var Chai = require('chai')
5
var expect = Chai.expect
6
7
var Factory = require('../../../../lib/Stub/Factory').Factory
8
9
describe('Integration', function () {
10
  describe('/Stub', function () {
11
    describe('/Factory.js', function () {
12
      describe('.Factory', function () {
13
        describe('.create()', function () {
14
          var handlers
15
          var properties
16
          var state
17
          var onFlush
18
          var Stub
19
20
          var factory = function (settings, symbol) {
21
            return Factory.create(symbol || 'Stub', settings)
22
          }
23
24
          var autoFactory = function (symbol) {
25
            var settings = {
26
              handlers: handlers,
27
              properties: properties,
28
              state: state,
29
              onFlush: onFlush
30
            }
31
            Stub = factory(settings, symbol)
32
            return Stub
33
          }
34
35
          var autoStub = function (symbol) {
36
            autoFactory(symbol)
37
            return new Stub()
38
          }
39
40
          it('creates instantiable stub class', function () {
41
            autoFactory()
42
            var lambda = function () {
43
              return new Stub()
44
            }
45
            expect(lambda).not.to.throw()
46
          })
47
48
          it('installs provided state', function () {
49
            state = {x: 12}
50
            expect(autoStub()).to.have.property('_state').deep.eq(state)
51
          })
52
53
          it('installs provided properties', function () {
54
            var property = {x: 12}
55
            properties = {prop: property}
56
            expect(autoStub()).to.have.property('prop').deep.eq(property)
57
          })
58
59
          it('installs provided handlers', function () {
60
            var handler = Sinon.spy(function () {})
61
            handlers = {handler: handler}
62
            var stub = autoStub()
63
            expect(stub).to.have.property('handler').instanceOf(Function)
64
            var arg = {x: 12}
65
            var thisValue = {x: 13}
66
            stub.handler.call(thisValue, arg)
67
            expect(stub.handler.callCount).to.eq(1)
68
            expect(handler.callCount).to.eq(1)
69
            expect(handler.getCall(0).args[0]).to.eq(stub)
70
            expect(handler.getCall(0).args[1]).to.eq(arg)
71
            expect(handler.getCall(0).thisValue).to.eq(thisValue)
72
          })
73
74
          it('reverts handlers on ._reset()', function () {
75
            var handler = Sinon.spy(function () {})
76
            handlers = {handler: handler}
77
            var stub = autoStub()
78
            expect(stub).to.have.property('handler').instanceOf(Function)
79
            stub.handler()
80
            expect(stub.handler.callCount).to.eq(1)
81
            expect(handler.callCount).to.eq(1)
82
            stub._reset()
83
            stub.handler()
84
            expect(stub.handler.callCount).to.eq(1)
85
            expect(handler.callCount).to.eq(2)
86
          })
87
88
          it('resets state on ._reset()', function () {
89
            state = {x: 12}
90
            var stub = autoStub()
91
            stub._state = 'twenty'
92
            stub._reset()
93
            expect(stub._state).to.deep.eq(state)
94
          })
95
96
          it('installs provided onFlush handler', function () {
97
            onFlush = Sinon.spy(function () {})
98
            var stub = autoStub()
99
            stub._flush()
100
            expect(onFlush.callCount).to.be.at.least(1)
101
            expect(onFlush.getCall(0).args[0]).to.eq(stub)
102
          })
103
104
          it('creates _flush even without provided handler', function () {
105
            onFlush = null
106
            expect(autoStub()._flush).not.to.throw()
107
          })
108
        })
109
      })
110
    })
111
  })
112
})
113